package game_states;


import java.awt.Image;
import java.util.ArrayList;


import game_objects.BackgroundScene;
import game_objects.Player;
import game_objects.PotHole;
import game_objects.Shark;

public class Game {

	//Instance Variables
	protected Main parent;
	protected Main p2;
	public FinishedScreen finishedScreen;
	private Player player;
	private BackgroundScene[] backgroundScene;
	private float backgroundScene0StartX,backgroundScene1StartX, backgroundScene2StartX;
	private float backgroundScene0EndX, backgroundScene1EndX, backgroundScene2EndX;
	private PotHole pothole;
	private boolean jump = false;
	private boolean doneJumping;
	private boolean gameHasNotStarted = true;
	public boolean playingGame;
	private boolean GameOver = false;
	private int caloriesburned;
	private float randomY, randomY2,randomY3; 
	private float sharkY,sharkY2, sharkY3;
	private String rank;
	private boolean shark2go = false, psychotrigger= false;
	private Shark target, target2, target3;
	private int savedTime;
	private int totalTime = 10000;
	private int crazytime = 20000;
	private int imgVersionNumb = 1;
	
	public Game (Main p)
	{	
		
		parent = p;
		finishedScreen = new FinishedScreen(p);
		p2 = p;
		playingGame = false;
		player = new Player(parent, parent.width / 8, parent.height-(player.getPLAYER_HEIGHT()*2.70f), player.getPLAYER_WIDTH(), player.getPLAYER_HEIGHT(), parent.loadImage("stickFig1.png"));
		randomY = parent.random(250,350);
		randomY2 = parent.random(250,350);
		randomY3 = parent.random(250,350);
		sharkY = randomY;
		sharkY2 = randomY2;
		sharkY3 = randomY3;
		
			
		target = new Shark(parent, player.getX()+720, sharkY, target.getSharkWidth(), target.getSharkHeight(), parent.loadImage("2035-white-shark-designbluesmaller.png"));
		target2 = new Shark(parent, player.getX()+1440, sharkY2, target2.getSharkWidth(), target2.getSharkHeight(), parent.loadImage("shark2smaller.png"));
		target3 = new Shark(parent, player.getX()+2180, sharkY3, target3.getSharkWidth(), target3.getSharkHeight(), parent.loadImage("hammerhead.png"));
		pothole = new PotHole(parent, player.getX()+720, 385f, pothole.getPotholeWidth(), pothole.getPotholeHeight(),parent.loadImage("potHole.png"));
	
		 savedTime = parent.millis();
		
		backgroundScene0StartX = 0;
		backgroundScene1StartX = 720;
		backgroundScene2StartX = backgroundScene1StartX*2;
		
		
		//stuff.add(target);
		//stuff.add(target2);
		//stuff.add(hammerheadShark);
		
		backgroundScene = new BackgroundScene[3];
		backgroundScene[0] = new BackgroundScene(parent, backgroundScene0StartX, 0, parent.WIDTH, parent.HEIGHT, parent.loadImage("background01.png"));
		backgroundScene[1] = new BackgroundScene(parent, backgroundScene1StartX, 0, parent.WIDTH, parent.HEIGHT, parent.loadImage("background02.png"));
		backgroundScene[2] = new BackgroundScene(parent, backgroundScene2StartX, 0, parent.WIDTH, parent.HEIGHT, parent.loadImage("background03.png"));
		
		backgroundScene0EndX = backgroundScene0StartX+backgroundScene[0].getWidth();
		backgroundScene1EndX = backgroundScene1StartX+backgroundScene[1].getWidth();
		backgroundScene2EndX = backgroundScene2StartX+backgroundScene[2].getWidth();
		
		parent.font = parent.createFont("Arial", 40, true);
	}
	
	public void draw()
	{
		parent.background(0);
		backgroundScene[0].draw(true);
//		System.out.println("X[0] being drawn at: " + backgroundScene[0].getX());
		backgroundScene[1].draw(true);
//		System.out.println("X[1] being drawn at: " + backgroundScene[1].getX());
		backgroundScene[2].draw(true);
//		System.out.println("X[2] being drawn at: " + backgroundScene[2].getX());
		player.draw(true);
		
		
		target.draw(true);
		pothole.draw(true);
		System.out.println(pothole.getPotX1()+" "+pothole.getPotY1());
		int passedTime = parent.millis() - savedTime;
		   //Has five seconds passed?
		  if (passedTime > totalTime) {
		   
		    shark2go = true;
		   
	    savedTime = parent.millis(); // Save the current time to restart the timer!
		  }
		  if (passedTime > crazytime) {
			   
			    psychotrigger = true;
			   
		    savedTime = parent.millis(); // Save the current time to restart the timer!
			  }
		  if (shark2go){
			 target3.draw(true);
		  }
		  if (psychotrigger){
			  target2.draw(true);
		  }
		  
		if (playingGame)
		{
			actions();
			simulatePlayerRunning();
			
			pothole.moveLeft();
			pothole.moveToLastXPosition();
			target3.moveToLastXPositionMod(player.getX()+800);
			target3.moveLeftShark3();
			target2.moveToLastXPosition(player.getX()+720);
			target2.moveLeftShark2();
			target.moveToLastXPosition(player.getX()+720); //everytime the Y changes change the setY in the shark method
			target.moveLeft();
			
			rectRectCollisionHasHappened(target.getSharkX1(), target.getSharkY1(), 
										 target.getSharkX2(), target.getSharkY2(), 
										 target.getWidth(), target.getHeight());
			rectRectCollisionHasHappened(target3.getSharkX1(), target3.getSharkY1(), 
				 						target3.getSharkX2(), target3.getSharkY2(), 
				 						target3.getWidth(), target3.getHeight());
			rectRectCollisionHasHappened(pothole.getPotX1(), pothole.getPotY1(), 
										pothole.getPotX2(), pothole.getPotY2(), 
										pothole.getWidth(), pothole.getHeight());
			
			caloriesburned++;
			if (caloriesburned < 1000){
				rank = "Rank 0: Chunky Cheez Burgers";
			}
			if (caloriesburned >= 1000){
				rank = "Rank: 1 - Wears running shoes cause they look nice";
			}
			if (caloriesburned >= 3000){
				rank = "Rank: 2 - Takes themself a little too seriously";
			}
			if (caloriesburned >= 5000){
				rank = "Rank: 3 - Runs because they have to";
			}
			if (caloriesburned >= 7000){
				rank = "Rank: 4 - Runs for fun";
			}
			if (caloriesburned >= 9000){
				rank = "Rank: 5 - Totally forgot theres a tornado behind me";
			}
			if (caloriesburned >= 12000){
				rank = "Rank: 6 - It's not about the runner, its about the runner inside the runner";
			}
			if (caloriesburned >= 15000){
				rank = "Rank: 7 -  It doesn't matter if this storm ever ends";
			}
			if (caloriesburned >= 18000){
				rank = "Rank: 8 - Make it Runnable";
			}
			if (caloriesburned >= 20000){
				rank = "Rank: 9 - Big Digit";
			}
			if (caloriesburned >= 25000){
				rank = "Rank: 10 - The D.P.";
			}
			if (parent.keyPressed && parent.key == 'p'){
				stop();
				}
			
			parent.textFont(parent.font, 40);
			parent.fill(0);
			parent.text("Calories Burned: " + caloriesburned, 60, 40);
			parent.text(rank, 60, 70);
			backgroundScene[0].moveLeft();
			backgroundScene[0].moveToLastXPosition(backgroundScene2EndX);

			backgroundScene[1].moveLeft();
			backgroundScene[1].moveToLastXPosition(backgroundScene0EndX);

			backgroundScene[2].moveLeft();
			backgroundScene[2].moveToLastXPosition(backgroundScene1EndX);

		}
		else if (gameHasNotStarted && playingGame == false)
		{
			//Text will notify user the necessary steps to start the game.
			parent.textFont(parent.font, 40);
			parent.fill(0);
			parent.text("Press 'C' or 'X' to start game!", 20, 40);
			if ((parent.keyPressed && parent.key == 'c') || (parent.keyPressed && parent.key == 'x'))
			{
				playingGame = true;
				gameHasNotStarted = false;
			}
		}
		if(GameOver) {
		finishedScreen.draw(caloriesburned, rank);
		

		}
	}

	public void stop()
	{
		playingGame = false;
		parent.text("Press C or X to unpause!", 20, 40);
		gameHasNotStarted = true;
		
	}
	//FIX the below method**
	//Creates the illusion that the player is running by flickering images
	public void simulatePlayerRunning()
	{
		if (imgVersionNumb<6)
		{
			imgVersionNumb+=1;
			player.setImage(parent.loadImage("stickFig" + imgVersionNumb + ".png"));
		}
		else if (imgVersionNumb>5)
		{
			imgVersionNumb-=1;
			player.setImage(parent.loadImage("stickFig" + imgVersionNumb + ".png"));
		}
		else if (imgVersionNumb<1)
		{
			imgVersionNumb+=1;
			player.setImage(parent.loadImage("stickFig" + imgVersionNumb + ".png"));
		}
		
	}
	public void actions ()
	{
		if (parent.keyPressed && parent.key == ' ')
		{
			jump = player.jump(doneJumping);
		}
		else if (jump)
		{
			player.draw(true);
		}
		else 
		{
			doneJumping = false;
			player.draw(true);
		}
	}
	
		public boolean rectRectCollisionHasHappened(float collidingX1, float collidingY1, float collidingX2, float collidingY2, float collidingWidth, float collidingHeight)
		{
			if ((player.getPlayerX1()>=collidingX1 && player.getPlayerX1()<=collidingX2) ||
					(player.getPlayerX2()>=collidingX1 && player.getPlayerX2()<=collidingX2)) 
			{
				System.out.println("Almost");
				if ((player.getPlayerY1()>=collidingY1 && player.getPlayerY1()<=collidingY2) ||
						(player.getPlayerY2()>=collidingY1 && player.getPlayerY2()<=collidingY2)) {
					System.out.println("A HIT HAS HAPPENED");
					playingGame = false;
					GameOver = true;	
						
					return true; // if a hit, return true
				}
				else { // if not, return false
					return false;
				}
			}
			return false;
		}
}
